﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseCore.Dao
{
    public class ProductSpecialDao
    {
        public List<ProductSpecialPercent> GetAll()
        {
            using (var ctx = new ExportDBEntities())
            {
                var product = (from x in ctx.ProductSpecialPercent
                               select x).ToList();

                return product;
            }

        }

        public List<ProductSpecialPercent> GetAll(bool isActive)
        {
            using (var ctx = new ExportDBEntities())
            {
                var product = (from x in ctx.ProductSpecialPercent
                               where x.IsActive == isActive
                               select x).ToList();

                return product;
            }

        }

        public bool Insert(ProductSpecialPercent entity)
        {
            using (var ctx = new ExportDBEntities())
            {
                ctx.ProductSpecialPercent.Add(entity);
                return ctx.SaveChanges() > 0;
            }
        }

        public bool Delete(int Id)
        {
            using (var ctx = new ExportDBEntities())
            {
                int result = ctx.Database.ExecuteSqlCommand("DELETE FROM ProductSpecialPercent WHERE Id = @Id ", new[] { new SqlParameter("@Id", Id) });
                return result > 0;
            }
        }

        public bool Update(ProductSpecialPercent entity)
        {
            using (var ctx = new ExportDBEntities())
            {
                ProductSpecialPercent product = (from x in ctx.ProductSpecialPercent
                                                 where x.Id == entity.Id
                                                 select x).FirstOrDefault();

                if (product == null)
                    return false;


                if (entity.PercentSpecial != -1)
                {
                    product.PercentSpecial = entity.PercentSpecial;
                }

                if (entity.StartDate != null)
                {
                    product.StartDate = entity.StartDate;
                }

                if (entity.EndDate != null)
                {
                    product.EndDate = entity.EndDate;
                }

                product.IsActive = entity.IsActive;

                return ctx.SaveChanges() > 0;
            }
        }


    }
}
